import {Component, EventEmitter, Input, OnInit, Output, SimpleChanges} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from "@angular/forms";
import {BaseService} from "../../../shared/service/base.service";
import {ApiUrl} from "../../../config/api-url";
import {DeviceStatusEnum} from "../../../enums/device-status.enum";

@Component({
  selector: 'app-batch-operation-status',
  templateUrl: './batch-operation-status.component.html',
  styleUrls: ['./batch-operation-status.component.less']
})
export class BatchOperationStatusComponent implements OnInit {
// 模态框显隐
  @Input()
  isVisible = false;
  @Input()
  inputDeviceListByUpdateStatus = [];
  @Input()
  total = 0;
  @Input()
  deviceAllSelected = false;
  @Input()
  deviceFormModel: FormGroup;
  // 父子互传值
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  // 提交时提交按钮是否加载中
  isOkLoading: any;
  // 模态框表单
  formModel: FormGroup;
  title: any;

  deviceStatus =true;

  deviceStatusEnum=DeviceStatusEnum;

  constructor(private fb: FormBuilder, private baseService: BaseService) { }

  ngOnInit(): void {
    this.buildForm();
  }
  buildForm() {
  }


  ngOnChanges(changes: SimpleChanges): void {
    try {
      this.deviceStatus = true;
    }catch (e) {
      console.log(e.msg);
    }
  }

  // 取消
  handleCancel() {
    this.isVisible = false;
    this.emit.emit(this.isVisible);
  }

  // 确认
  handleOk() {
    this.isOkLoading = true;

    const params = {
      ...this.deviceFormModel.value, // 将 deviceFormModel 的属性合并到 params 对象中
      updateStatus: this.deviceStatus == true ? this.deviceStatusEnum.ON_LINE : this.deviceStatusEnum.OFF_LINE,
      deviceIds: this.inputDeviceListByUpdateStatus,
      deviceAllSelected: this.deviceAllSelected
    };

    this.baseService.post(ApiUrl.DeviceUrl + '/batchUpdateStatus', params).subscribe(res => {
      if (res.status == 0) {
        this.baseService.layer.messageSuccess("修改成功");
        this.inputDeviceListByUpdateStatus = [];
        this.handleCancel();
        this.isOkLoading = false;
      } else {
        this.baseService.layer.messageError(res.msg);
        this.isOkLoading = false;
      }
    });
  }
}
